<?php
/**
 * Author:   tangxiaowen
 * Email:    846506584@qq.com
 * Date:     2019/3/12 0012
 * Describe: 借款产品管理类
 */

namespace app\index\controller;

class Goods extends Base
{
    /**
     * 获取所有借款产品数据
     */
    public function getAllGoods()
    {
        $goodsAllData = cache('goodsAllData');
        if(!$goodsAllData){
            $goodsAllData = db('goods')->select();
            cache('goodsAllData',$goodsAllData,1);
            $goodsAllData = cache('goodsAllData');
        }
        return $goodsAllData;
    }

    /**
     * 检测产品是否允许续期
     * @param int $orderInfo 订单信息[产品ID]
     * @return true|1|0
     */
    public function isOrderRenewal($orderInfo)
    {
        //检测订单是否逾期
        $isOrderOverdue = (new Order())->isOrderOverdue($orderInfo);
        $tempStr = $isOrderOverdue?'Statusyuqi':'Status';

        //产品不存在无法判断是否允许续期，采取默认允许续期
        $goodsInfo = $this->getGoodsInfo($orderInfo['ProductID']);
        if(!$goodsInfo) return true;

        return $goodsInfo[$tempStr];
    }

    /**
     * 获取产品信息
     * @param int $goodsId 产品ID
     * @return array|false
     */
    public function getGoodsInfo($goodsId)
    {
        $goodsAllData = $this->getAllGoods();

        foreach($goodsAllData as $k=>$v){
            if($v['ID'] == $goodsId){
                return $v;
            }
        }

        return false;
    }

}